#import "MRSkinManager.h"

NSString * const MRSkinChangeNotification	= @"skin_change";

@implementation MRSkinManager
{
	UIColor		*_userColor;
}

+ (instancetype)skin
{
	return skin;
}

/*===================================================================
 说明:
	类化
 ==================================================================*/
+ (instancetype)skinWithColor:(UIColor *)color
{
	MRSkinManager *_skin = [[MRSkinManager alloc] init];
	_skin->_userColor = color;
	_skin.nightMode = NO;
	
	return _skin;
}

/*===================================================================
 说明:
	切换夜间主题模式
 ==================================================================*/
- (void)setNightMode:(BOOL)nightMode
{
	_nightMode = nightMode;
	
	_skinColor = _userColor;
	if (!_skinColor) _skinColor = rgb(240, 60, 20);		//默认主题颜色
	
	if (self.nightMode)		//夜间主题
	{
		_backgroundColor = rgb(35, 35, 36);
		_mainTextColor   = [UIColor whiteColor];
		_statusBarStyle  = UIStatusBarStyleLightContent;
		
		[UINavigationBar appearance].barTintColor = _userColor;
		[UINavigationBar appearance].barStyle = UIBarStyleBlack;
		[UINavigationBar appearance].tintColor = [UIColor whiteColor];
		
		[UITabBar appearance].barStyle  = UIBarStyleBlack;
		[UITabBar appearance].tintColor = [UIColor whiteColor];
		//[UITabBar appearance].barTintColor = [UIColor blackColor];
		[UIToolbar appearance].barStyle = UIBarStyleBlack;
		[UIToolbar appearance].tintColor = [UIColor whiteColor];
		
		[UISearchBar appearance].barStyle = UIBarStyleBlack;
		
		//[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:rgb(220, 220, 0)} forState:UIControlStateNormal];
		
		[UITableView appearance].separatorColor  = rgb(80, 80, 80);
		[UITableViewCell appearance].backgroundColor = rgb(50, 50, 52);
		
		UIView *view = [UIView viewWithColor:rgb(40, 40, 41)];
		[UITableViewCell appearance].selectedBackgroundView = view;
		
		[UISwitch appearance].tintColor = [UIColor darkGrayColor];
		[UISwitch appearance].thumbTintColor = rgb(220, 220, 220);
		
		[UIActivityIndicatorView appearance].color = [self colorWithIndex:MRSkinColorGrayText];
		
		//文本颜色
		[UILabel appearanceWhenContainedIn:[UITableViewCell class], nil].backgroundColor = [UIColor clearColor];
		
		[UITextView appearance].textColor = [UIColor whiteColor];
		[UITextField appearance].textColor = [UIColor whiteColor];
	}
	else	//白天模式
	{
		_statusBarStyle  = UIStatusBarStyleLightContent;
		_backgroundColor = [UIColor groupTableViewBackgroundColor];
		_mainTextColor   = [UIColor blackColor];
		
		[UINavigationBar appearance].tintColor = [UIColor whiteColor];
		[UINavigationBar appearance].barTintColor = _skinColor;
		[UINavigationBar appearance].barStyle = UIBarStyleBlack;
		
		[UISearchBar appearance].barStyle = UIBarStyleDefault;
		
		[UITabBar appearance].barStyle = UIBarStyleDefault;
		//[UITabBar appearance].barTintColor = rgba(255, 255, 255, .1);
		[UIToolbar appearance].barStyle = UIBarStyleDefault;
		
		[UITableView appearance].separatorColor = rgb(230, 230, 230);
		[UITableViewCell appearance].backgroundColor = rgba(247, 247, 250, 1);
		
		[UIActivityIndicatorView appearance].color = [self colorWithIndex:MRSkinColorGrayText];
	}
	
	[UITableView appearance].backgroundColor = [UIColor whiteColor];
	
	[UITabBar appearance].tintColor = _skinColor;
	[UITextView appearance].tintColor = _skinColor;
	[UITextView appearance].textContainerInset = UIEdgeInsetsMake(11, 11, 11, 11);
	
	[[UIButton appearance] setTitleColor:_skinColor forState:UIControlStateSelected];
	[UIButton appearanceWhenContainedIn:[UITableView class], nil].tintColor = _skinColor;
	
	[UISwitch appearance].onTintColor = _skinColor;
	//[UICollectionView appearance].backgroundColor = [UIColor clearColor];
	
	[UIProgressView appearance].tintColor = _skinColor;
	
	[UISegmentedControl appearance].tintColor = _skinColor;
	//[[UISegmentedControl appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} forState:UIControlStateNormal];
	[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateSelected];
	
	NSMutableDictionary *attr = [NSMutableDictionary dictionaryWithDictionary:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
	[[UIBarButtonItem appearance] setTitleTextAttributes:attr forState:UIControlStateNormal];
	
	attr[NSForegroundColorAttributeName] = [skin.mainTextColor colorWithAlphaComponent:.9];
	[[UIBarButtonItem appearanceWhenContainedIn:[UIToolbar class], nil] setTitleTextAttributes:attr forState:UIControlStateNormal];
	
	[[NSNotificationCenter defaultCenter] postNotificationName:MRSkinChangeNotification object:self];
}

#pragma mark - 获取颜色
/*===================================================================
 说明:
	获取指定主题索引的颜色
 ==================================================================*/
- (UIColor *)colorWithIndex:(MRSkinColorIndex)index
{
	switch (index)
	{
		case MRSkinColorDestructive:return rgb(255, 60, 30);
		case MRSkinColorSkin:		return _skinColor;
			
		case MRSkinColorMainText:	return _mainTextColor;
		case MRSkinColorDarkText:	return [_mainTextColor colorWithAlphaComponent:.667];
		case MRSkinColorGrayText:	return [_mainTextColor colorWithAlphaComponent:.5];
		case MRSkinColorLightText:	return [_mainTextColor colorWithAlphaComponent:.333];
		case MRSkinColorAlphaText:	return [_mainTextColor colorWithAlphaComponent:.1];
	}
}

//用户性别识别色
- (UIColor *)colorForGender:(NSUInteger)gender
{
	return gender ? rgba(255, 100, 150, .7) : rgba(50, 190, 255, .7);
}

//运动类型的主色调
- (UIColor *)colorForSportType:(FSSportType)type
{
	switch (type)
	{
		case FSSportTypeFree:		return rgb(127, 127, 127);
			
		case FSSportTypeTreadmill:	return rgb(240, 60, 0);
		case FSSportTypeElliptical:	return rgb(120, 180, 0);
		case FSSportTypeBicycle:	return rgb(60, 200, 0);
		
		case FSSportTypeWalking:	return rgb(240, 180, 0);
		case FSSportTypeRunning:	return rgb(240, 70, 100);
		case FSSportTypeCycling:	return rgb(0, 220, 100);
		case FSSportTypeRoller:		return rgb(50, 200, 255);
		case FSSportTypeSwimming:	return rgb(180, 70, 140);
		case FSSportTypeRowing:		return rgb(60, 200, 180);
		case FSSportTypeSkiing:		return rgb(60, 120, 220);
		case FSSportTypeClimbing:	return rgb(255, 150, 50);
		case FSSportTypeRiding:		return rgb(180, 40, 255);
			
		case FSSportTypeDriving:	return rgb(255, 128, 0);
			
		default: return _skinColor;
	}
}

@end

#pragma mark - UILabel
/*===================================================================
 说明:
	用系统视图定制主题
 ==================================================================*/
@implementation UILabel (MRSkinManager)
- (void)awakeFromNib
{
	[super awakeFromNib];
	
	if (self.tag < 0)
	{
		self.textColor = [skin colorWithIndex:self.tag];
	}
	else if ([MRSkinManager skin].nightMode)
	{
		CGFloat r, g, b, a;
		[self.textColor getRed:&r green:&g blue:&b alpha:&a];
			
		if (r == 0 && g == 0 && b == 0 && a == 1) self.textColor = [MRSkinManager skin].mainTextColor;
	}
}

- (void)didLanguage
{
	self.text = L(self.language);
}

@end

#pragma mark - UITextField
@implementation UITextField (MRSkinManager)
- (void)awakeFromNib
{
	[super awakeFromNib];
	
	if (self.tag < 0)
	{
		self.textColor = [skin colorWithIndex:self.tag];
	}
	
	[self setValue:[[MRSkinManager skin] colorWithIndex:MRSkinColorLightText] forKeyPath:@"_placeholderLabel.textColor"];
}

- (void)didLanguage
{
	self.placeholder = L(self.language);
}

@end

#pragma mark - UITextView
@implementation UITextView (MRSkinManager)
- (void)awakeFromNib
{
	[super awakeFromNib];
	
	if (self.tag < 0)
	{
		self.textColor = [skin colorWithIndex:self.tag];
	}
}

@end

#pragma mark - UISearchBar
@implementation UISearchBar (MRSkinManager)
- (void)awakeFromNib
{
	[super awakeFromNib];
	
	UITextField *searchField = [self valueForKey:@"_searchField"];
	searchField.textColor = [MRSkinManager skin].mainTextColor;
	[searchField setValue:[[MRSkinManager skin] colorWithIndex:MRSkinColorLightText] forKeyPath:@"_placeholderLabel.textColor"];
}

@end
